package com.m7788.screen;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.custom.lpulllist.PullToRefreshListView;
import com.imagemanager.util.CacheService;
import com.m7788.adapter.LettersAdapter;
import com.m7788.util.AppUtil;
import com.m7788.util.Constant;
import com.m7788.view.UmengBaseActivity;
import com.tool.media.Image;
import com.tool.media.ImageProvider;
import com.tool.utils.DataUtil;

public class DirectoryScreen extends UmengBaseActivity implements OnClickListener{
	private Button btn_left;
	private TextView tv_result;
	private ArrayList<Image> dataList = new ArrayList<Image>();
	private PullToRefreshListView refreshLV;
	private LettersAdapter lvAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.directory_screen);
		ImageProvider ip  = new ImageProvider(this);
		btn_left = (Button) this.findViewById(R.id.ds_btn_left);
		btn_left.setOnClickListener(this);
		tv_result = (TextView) this.findViewById(R.id.ds_text_title);
		
		refreshLV = (PullToRefreshListView) findViewById(R.id.ds_listview);
		
		dataList  = ip.getFolderList();
		String config = AppUtil.getRecord(this, Constant.DIRECTORYCONFIG);
		for(Image i : dataList){
			String p = i.getPath().substring(0, i.getPath().lastIndexOf("/"));
			if(config.indexOf(p+":")>0){
				i.setCheck(true);
			}
			for(String a :CacheService.dcimFolderList){
				if(a.equals(p)){
					i.setCheck(true);
				}
	        }
		}
		
		
		
		lvAdapter = new LettersAdapter(this, dataList);// 这里为空也要设置为了让上面的底部view显示出来
		refreshLV.setAdapter(lvAdapter);
		refreshLV.setCanPullRefresh(false);
		
		refreshLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 )
					return;
				
				Image info = lvAdapter.getItem(position-1);
				if(info.getPath().indexOf("DCIM")>0 || info.getPath().indexOf("dcim")>0 ){
					DataUtil.showShortToast(DirectoryScreen.this, "dcim所属文件夹不允许操作");
					return;
				}
				lvAdapter.changeCheck(info);
				lvAdapter.notifyDataSetChanged();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
	    	case R.id.ds_btn_left:
	    		DataUtil.showShortToast(DirectoryScreen.this, "修改后的文件夹图片将在下次启动后生效");
				
	    		finish();
	    		break;
	    	case R.id.ds_btn_submit:
	    		break;	
		}
	}
}
